package com.xyht.sca_s.student_manage_system.modules.trainingSchool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.SmsTsUnitPlace;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.entity.resp.SmsTsUnitPlaceResp;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.mapper.SmsTsUnitPlaceMapper;
import com.xyht.sca_s.student_manage_system.modules.trainingSchool.service.SmsTsUnitPlaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 单位属地表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-05-28
 */
@Service
public class SmsTsUnitPlaceServiceImpl extends ServiceImpl<SmsTsUnitPlaceMapper, SmsTsUnitPlace> implements SmsTsUnitPlaceService {

    @Override
    public ResponseResult list(Integer pageNum, Integer pageSize, String ss) {
        LambdaQueryWrapper<SmsTsUnitPlace> queryWrapper = new LambdaQueryWrapper<SmsTsUnitPlace>()
                .orderByAsc(SmsTsUnitPlace::getCreateTime);

        if (!isNullOrEmpty(ss)) {
            queryWrapper.like(SmsTsUnitPlace::getName, ss);
        }

        Page<SmsTsUnitPlace> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        List<SmsTsUnitPlaceResp> collect = page.getRecords().stream().map(item -> {
            SmsTsUnitPlaceResp resp = new SmsTsUnitPlaceResp();
            BeanUtils.copyProperties(item, resp);
            return resp;
        }).collect(Collectors.toList());

        return CommonResult.success(collect, (int) page.getTotal());
    }
}
